/*
 * @Author: zhouhai zhouhaib@yonyou.com
 * @Date: 2025-03-04 11:19:08
 * @LastEditors: zhouhai zhouhaib@yonyou.com
 * @LastEditTime: 2025-03-04 11:32:40
 */
import React from 'react';

// 懒加载组件

// 定义一个 ErrorBoundary 组件
interface ErrorBoundaryProps {
    fallback: React.ReactNode;
    children: React.ReactNode;
}

interface ErrorBoundaryState {
    hasError: boolean;
}

export class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
    constructor(props: ErrorBoundaryProps) {
        super(props);
        this.state = { hasError: false };
    }

    componentDidCatch(error: any, errorInfo: any) {
        // 捕获错误并更新状态
        this.setState({ hasError: true });
        // 可以在这里记录错误信息
        console.log('Error loading lazy component:', error, errorInfo);
    }

    render() {
        if (this.state.hasError) {
            // 加载失败时渲染默认组件
            return <div>{this.props.fallback}</div>;
        }
        return this.props.children;
    }
}